#include <iostream>
using namespace std;

class Base {
public:
    virtual void print() {
        cout << "Base" << endl;
    }
};

class Derived : public Base {
public:
    void print() {
        cout << "Derived" << endl;
    }
};

int main() {
    Base* basePtr = new Derived;
    Derived* derivedPtr = dynamic_cast<Derived*>(basePtr); // 向下转换

    if (derivedPtr) {
        derivedPtr->print(); // 输出Derived
    } else {
        cout << "Failed to cast" << endl;
    }

    return 0;
}

/*
在这个示例中，`dynamic_cast`被用于将指向派生类对象的指针`basePtr`转换为指向派生类对象的指针`derivedPtr`，然后调用`print`方法输出`"Derived"`。
*/